// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

export enum handType{
    click = 0,
    move = 1,
    scale = 2,
    guide = 3,
    scaleOnce = 4,
    rotate = 5,
    scaleCustomer = 6,
    rotateScale = 7,
    downScale = 8,
    showAndHide = 9,
    rotateByRotate
}

@ccclass
export default class handMediator extends cc.Component {

    @property({type : cc.Enum(handType),displayName : "手势动画类型"})
    handAnimation : handType = handType.click;

    @property({displayName : "maxScale"})
    maxScale : number = 0.1;

    isRotate = false;

    isRotateScale = false;

    curScale = 0;

    onLoad () {
        switch (this.handAnimation) {
            case handType.click :
                this.handAction();
                break;
            case handType.move :
                this.handMove();
                break;
            case handType.scale :
                this.scaleAction();
                break;
            case handType.guide :
                this.handGuide();
                break;
            case handType.scaleOnce :
                this.handScaleOnce();
                break;
            case handType.rotate :
                this.isRotate = true;
                break;
            case handType.scaleCustomer :
                this.scaleActionCustomer();
                break;
            case handType.rotateScale :
                this.curScale = this.node.scale;
                this.isRotateScale = true;
                break;
            case handType.downScale :
                this.scaleActionCustomer2();
                break;
            case handType.showAndHide :
                this.showWithHide();
                break;
            case handType.rotateByRotate :
                this.rotateByRotate();
                break;
        }
    }

    rotateByRotate(){
        cc.tween(this.node)
            .to(0.1,{angle : -8})
            .to(0.3,{angle : 0})
            .delay(0.4)
            .call(()=>{
                this.rotateByRotate();
            })
            .start();
    }

    showWithHide(){
        cc.tween(this.node)
            .to(0.5,{
                opacity : 0})
            .to(0.5,{
                opacity : 255
            })
            .call(()=>{
                this.showWithHide();
            })
            .start();
    }

    handGuide(){
        cc.loader.loadResDir("texture/hand",cc.SpriteFrame,(err,res)=>{
            let arr = [];
            for(let j = 0 ; j < res.length ; j++){
                for(let i in res){
                    if(j <= 9){
                        if(res[i].name == "01_0000" + j){
                            arr.push(res[i]);
                        }else{
                            continue;
                        }
                    }else{
                        if(res[i].name == "01_000" + j){
                            arr.push(res[i]);
                        }else{
                            continue;
                        }
                    }
                }
            }
            let order = 0;
            this.schedule(()=>{
                this.node.getComponent(cc.Sprite).spriteFrame = arr[order];
                order++;
                if(order >= arr.length){
                    order = 0;
                }
            },0.04)
        })
    }


    scaleActionCustomer(){
        let initScale = this.node.scale;
        cc.tween(this.node)
            .to(0.5,{
                scale : initScale + this.maxScale})
            .to(0.5,{
                scale : initScale
            })
            .call(()=>{
                this.scaleActionCustomer();
            })
            .start();
    }

    scaleActionCustomer2(){
        let initScale = this.node.scale;
        let initY = this.node.y;
        cc.tween(this.node)
            .to(0.5,{
                scale : initScale + this.maxScale})
            .to(0.5,{
                scale : initScale
            })
            .call(()=>{
                this.scaleActionCustomer2();
                this.node.y = initY + 100;
            })
            .start();

        cc.tween(this.node)
            .by(0.5,{y : -100})
            .by(0.5,{y : 100})
            .start()
    }

    scaleAction(){
        let initScale = this.node.scale;
        cc.tween(this.node)
            .to(0.5,{scale : initScale + 0.1})
            .to(0.5,{scale : initScale})
            .call(()=>{
                this.scaleAction();
            })
            .start();
    }


    handMove(){
        let pos = this.node.position;
        let action1 = cc.bezierTo(1.8,[cc.v2(0,0),cc.v2(-100,180),cc.v2(240,30)]);
        this.node.runAction(cc.sequence(action1,cc.callFunc(()=>{
            this.node.setPosition(cc.v2(pos.x,pos.y));
            this.handMove();
        })));
        let picAction = ()=>{
            if(this.node.parent.getComponent(cc.Sprite).fillRange >= 1){
                this.unschedule(picAction);
                this.node.parent.getComponent(cc.Sprite).fillRange = 0;
            }else{
                this.node.parent.getComponent(cc.Sprite).fillRange += 0.01;
            }
        };
        this.schedule(picAction,0.01);
    }

    handAction(){
        cc.tween(this.node)
            .to(0.5,{scale : 0.2})
            .to(0.5,{scale : 0.15})
            .call(()=>{
                this.handAction();
            })
            .start();
    }

    handScaleOnce(){
        let scale = this.node.scale;
        cc.tween(this.node)
            .to(0.5,{scale : scale + 0.1})
            .to(0.5,{scale : scale})
            .start();
    }


    addDir = 1;
    update (dt) {
        if(this.isRotate){
            this.node.angle += 1;
        }

        if(this.isRotateScale){
            this.node.angle += 1;

            if(this.node.scale >= this.curScale + 0.2){
                this.addDir = -1;
            }else if(this.node.scale <= this.curScale){
                this.addDir = 1;
            }
            this.node.scale += this.addDir * 0.01;
        }
    }
}
